package cn.mineserv.video.videoordersystem.utils;

public class DeepCopyUtil {
    public static <T> T copy(T src) {
        return copy(src, src.getClass());
    }

    public static <T> T copy(T src, Class clazz) {
        T dest = null;
        try {
            dest = (T) clazz.newInstance();
            copy(src, dest);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return dest;
    }

    public static <T> void copy(T src, T dest) {
        if (src == null || dest == null) {
            return;
        }
        if (src.getClass() != dest.getClass()) {
            return;
        }
        Class<?> clazz = src.getClass();
        while (clazz != null) {
            for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    Object value = field.get(src);
                    field.set(dest, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            clazz = clazz.getSuperclass();
        }
    }

}
